<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval( getvar( "gametype" ) );
$chkGametype = chkGametype( $gametype );
if ( 2 < $chkGametype )
{
    exit();
}
$chk = new CheckMode_3d( $curruser['acl'], 3, $gametype );
$chk->checkShow3dPage();
$show5N = getShow35( $gametype );
$betname_3d = judge_type( $gametype );
unset( $chk );
$tblName = $g_gametype_3d[$gametype]['ch'];
if ( $curruser['acl'] != 255 || $curruser['prev_id'] != 0 )
{
    exit();
}
$ui = new UI_3D( true );
$tpl = new Template2();
$conf = new Config();
include( "class_3d/SRate_3d.php" );
require( "class_3d/SRateTbl_3d.php" );
include( "class_3d/RateConfig_3d.php" );
if ( $show5N )
{
    $SRateTbl_3d = new SRateTbl_5n( $gametype );
    $show5NStr = "true";
}
else
{
    $SRateTbl_3d = new SRateTbl_3n( $gametype );
    $show5NStr = "false";
}
$SRateTbl_3d->initData( $gametype );
$SRate_3d = $SRateTbl_3d;
$RateConfig_3d = new RateConfig_3d( $gametype );
$today = date( "Y-m-d" );
$disk = getvar( "disk" );
$playtype = getvar( "playtype" );
$bid = getvar( "bid" );
$lotot_id = intval( getvar( "id" ) );
if ( $lotot_id < 1 )
{
    $ui->showErrorPage( "错误的期数ID" );
    exit();
}
$lottoData = $SRate_3d->getLotto3DById( $lotot_id );
if ( $lottoData == false )
{
    $ui->showErrorPage( "无该期数资料" );
    exit();
}
$SRateTbl_3d->sLottoId = $lotot_id;
$fieldArr = array( "method" => "5", "gametype" => $gametype, "round" => $lottoData['round'], "lotto_id" => $lottoData['id'], "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "1", "paperopen" => $lottoData['open'] );
$msg = "";
$disk = $disk == "" ? "a" : $disk;
$playtype = isset( $RateConfig_3d->playtypeArr02[$playtype] ) ? $playtype : "r1";
$bidstr = "";
if ( $bid == "" )
{
    $bidstr = "";
}
else
{
    $bid == intval( $bid );
    if ( !in_array( $bid, $RateConfig_3d->playGroupArr02[$playtype] ) )
    {
        diemsg( "玩法资料错误。" );
    }
    $bidstr = "&bid=".$bid;
    $tpl->set( "bid", $bid );
}
if ( !$RateConfig_3d->checkDiskType( $disk ) )
{
    diemsg( "不合法的盘类".$disk );
}
$diskIdx = $RateConfig_3d->disk_idx[$disk];
if ( $bid != "" )
{
    $fieldsArr = array( $bid );
}
else
{
    $fieldsArr = $RateConfig_3d->playGroupArr02[$playtype];
}
if ( in_array( $playtype, $RateConfig_3d->showAllSubArr02 ) )
{
    $subFlag = 1;
    $subid = intval( getvar( "subid" ) );
    if ( in_array( $playtype, array( "r2", "r3" ) ) )
    {
        $subFlag = 0;
    }
}
else
{
    $subFlag = 0;
    $subid = 0;
}
if ( !isset( $RateConfig_3d->subArr[$subid] ) )
{
    diemsg( "不合法的类别" );
}
$SRateTbl_3d->subid = $subid;
$alertMsg = "";
if ( ispost( ) )
{
    $okI = 0;
    if ( isset( $_POST['rebackRateAll'] ) && $_POST['rebackRateAll'] == "r" )
    {
        $SRate_3d->updateNowRatetoBaseRate( $lottoData['id'] );
        $fieldArr['betcontent'] = "第".$lottoData['round']." 期含之後赔率回复";
        $fieldArr['orate'] = "0";
        $fieldArr['nrate'] = "0";
        setRateRecord( $fieldArr );
        $alertMsg = "alert('已将【".$g_gametype_3d[$gametype]['ch']."】第 ".$lottoData['round']." 期含之後赔率回复成基本赔率值');";
    }
    else
    {
        $tempStr = "";
        $subIdArr = array( 0 );
        foreach ( $fieldsArr as $val )
        {
            if ( $show5N && in_array( $val, $g_n3n5Arr ) )
            {
                if ( $subFlag )
                {
                    $subIdArr = array( $subid );
                }
                else
                {
                    $subIdArr = array( 2, 1, 0 );
                }
            }
            else
            {
                $subIdArr = array( 0 );
            }
            if ( isset( $SRate_3d->betForNumTbl[$val] ) )
            {
                $betTypeNumber = array();
                foreach ( $SRate_3d->betForNumTbl[$val] as $sKey => $sVal )
                {
                    $betTypeNumber[] = intval( substr( $sKey, 1 ) );
                }
            }
            else
            {
                $betTypeNumber = $RateConfig_3d->getBetTypeNumber( $val );
            }
            foreach ( $subIdArr as $subIdArrKey => $subIdArrVal )
            {
                $ratedata = array();
                $rateBaseArr = $SRate_3d->getRates( $lotot_id, $diskIdx, $val, $subIdArrVal );
                $rateBaseArr02 = $SRate_3d->getRates( 0, $diskIdx, $val, $subIdArrVal );
                foreach ( $betTypeNumber as $key2 => $val2 )
                {
                    $tempStr = "n_{$val}_{$subIdArrVal}_{$diskIdx}_{$val2}";
                    if ( isset( $_POST[$tempStr] ) )
                    {
                        $key2Idx = "r".str_pad( $val2, 2, 0, STR_PAD_LEFT );
                        $floatval = min( $rateBaseArr02[$key2Idx], floatval( $_POST[$tempStr] ) );
                        $floatval = 0 < $floatval ? $floatval : "0";
                        $ratedata[$key2Idx] = $floatval;
                        if ( $floatval != $rateBaseArr[$key2Idx] )
                        {
                            $tempId = $val."_".$subIdArrVal;

                            $fieldArr['betcontent'] = "[{$betname_3d[$tempId]}]".$RateConfig_3d->getBetContent( $val, $val2, true )." :: ".$g_disk_name[$diskIdx];
                            $fieldArr['orate'] = $rateBaseArr[$key2Idx];
                            $fieldArr['nrate'] = $floatval;
                            setRateRecord( $fieldArr );
                        }
                        ++$okI;
                    }
                    else
                    {
                        diemsg( "更新失败{$tempStr}", "玩法资料与内容不齐全，无法更新", "op.php?op=admin&fp=rate_3d&disk={$disk}&playtype={$playtype}&gametype={$gametype}&id={$lotot_id}&subFlag={$subFlag}&subid={$subid}" );
                    }
                }
                if ( count( $ratedata ) )
                {
                    $SRate_3d->updateRate( $lotot_id, $diskIdx, $val, $ratedata, $subIdArrVal );
                }
            }
        }
    }
    if ( 0 < $okI )
    {
        $msg = $tblName."&nbsp;::&nbsp;第".$lottoData['round']."期&nbsp;::&nbsp;".$RateConfig_3d->playtypeArr02[$playtype]."页面&nbsp;::&nbsp;".$g_disk_name[$diskIdx]."&nbsp;::&nbsp;"."异动成&#21151;  (".date( "Y-m-d H:i:s" ).")";
    }
    else
    {
        $msg = "";
    }
}
$SRateTbl_3d->reloadUrl = "op.php?op=admin&fp=rate_3d&disk={$disk}&playtype=".$playtype.$bidstr."&gametype={$gametype}&id={$lotot_id}&subFlag={$subFlag}&subid={$subid}";
$SRateTbl_3d->subURL = "op.php?op=admin&fp=rate_3d&disk={$disk}&playtype=".$playtype.$bidstr."&gametype={$gametype}&id={$lotot_id}&subFlag=1";
foreach ( $fieldsArr as $val )
{
    if ( $show5N && in_array( $val, $g_n3n5Arr ) )
    {
        $subIdArr = array( 2, 1, 0 );
    }
    else
    {
        $subIdArr = array( 0 );
    }
    foreach ( $subIdArr as $subIdArrKey => $subIdArrVal )
    {
        $rate_data = $SRate_3d->getRates( $lotot_id, $diskIdx, $val, $subIdArrVal );
        if ( $rate_data == false )
        {
            diemsg( "查无该期赔率资料" );
        }
    }
}
$jslink = "<script>";
$jslink .= "var _myGlobalBetStr = \"".$playtype."\";";
$jslink .= "var _myGlobal5NStr = \"".$show5NStr."\";";
$jslink .= "$(function(){ $().chgIptClr();   ";
if ( $playtype == "r2p" )
{
    $jslink .= "\r\n$(\"#btnR2pType input:button\").click(function(){\r\n  var bid = $(this).attr(\"id\").split(\"_\")[1];\r\n  location.replace(\"op.php?op=admin&fp=rate_3d&playtype=".$playtype."&gametype=".$gametype."&id=".$lotot_id."&disk=".$disk."&bid=\" + bid);\r\n  });\r\n";
}
$jslink .= "});</script>";
$jslink .= "<script src=\"js_3d/jsRate3d.js\"></script>";
$jslink .= "<script src=\"js_3d/showRateRecord.js\"></script>";
$jslink .= "<script src=\"js_3d/js3d.js\"></script>";
$ui->set( "jslink", $jslink );
$ui->header();
$disks = "<select onchange=\"location.replace('op.php?op=admin&fp=rate_3d&playtype=".$playtype.$bidstr."&gametype={$gametype}&id={$lotot_id}&disk='+this.value)\">";
$disks .= "<option value=a".( $disk == "a" ? " selected" : "" ).">A 盘</option>";
$disks .= "<option value=b".( $disk == "b" ? " selected" : "" ).">B 盘</option>";
$disks .= "<option value=c".( $disk == "c" ? " selected" : "" ).">C 盘</option>";
$disks .= "<option value=d".( $disk == "d" ? " selected" : "" ).">D 盘</option>";
$disks .= "</select>";
$playtypeArr = $RateConfig_3d->playtypeArr02;
$playMenu = "";
foreach ( $playtypeArr as $key => $val )
{
    if ( $key == $playtype )
    {
        $playMenu .= "<span class=\"cbig\">{$val}</span>&nbsp;|&nbsp;";
    }
    else
    {
        $playMenu .= "<a href=op.php?op=admin&fp=rate_3d&gametype={$gametype}&id={$lotot_id}&playtype=".$key.">".$val."</a>&nbsp;|&nbsp;";
    }
}
$playMenu .= "<a href=op.php?op=admin&fp=history_3d&gametype={$gametype}>回列表</a>";
$playMenu .= "<input type=\"button\" value=\"赔率异动纪录\" class=\"nobtnclass\" onclick=\"javascript:showRateRecordPage({$gametype},'".$lottoData['open']."');\"  />";
$g_5number_3dStrTitle="";
echo "<div id=\"divRateRecord\"></div>";
echo title( $playtypeArr[$playtype]." {$g_5number_3dStrTitle}赔率设定-第<b>".$lottoData['round']."</b>期，开奖日期：".$lottoData['open']." ".$disks." <input type=\"button\" id=\"btnRebackRateAll\" value=\"同步基本赔率\" class=\"nobtnclass\" />", "", "920" );
echo title( $playMenu, "", "920" );
echo $msg == "" ? "" : "<div class='msgBox-yellow' style='width:920px;'>{$msg}</div>";
echo "<br />";
echo "<style> thead, th{font-size:0.9em;} </style>";
echo "<form action='op.php?op=admin&fp=rate_3d&disk={$disk}&playtype={$playtype}{$bidstr}&gametype={$gametype}' method='post'>";
echo "<input type='hidden' value='{$lotot_id}' name='id' />";
echo $SRateTbl_3d->show_srateTbl( $playtype, $diskIdx, $subid, $bid );
echo "</form>";
echo "</td></tr></table>";
echo "<br />";
echo "<span> ※ 快调时, <font style='background-color:#FF9291'>浅红底</font>表示赔率小於 1 </span>";
echo "<br /><span> ※ {$tblName}玩法，可设定赔率至小数点下第三位。</span>";
echo "<script>";
echo $alertMsg;
echo "$(function(){\r\n  $(\"input#btnRebackRateAll\").click(function(){\r\n    if(confirm(\"确定要将 【".$g_gametype_3d[$gametype]['ch']."】 第".$lottoData['round']." 期含之後回复成基本赔率值吗(包含所有盘类)?\")){\r\n      $(\"form#frmbtnRebackRateAll\").submit();  \r\n    }\r\n  });\r\n});\r\n</script>\r\n";
echo "<form id=\"frmbtnRebackRateAll\" action=\"op.php?op=admin&fp=rate_3d&disk=".$disk."&playtype=".$playtype.$bidstr."&gametype=".$gametype."&id=".$lotot_id."\" method=\"post\"><input type=\"hidden\" value=\"r\" name=\"rebackRateAll\" /></form>";
$ui->footer();
?>